package RECUR_107;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import LCommon.TreeNode;

public class Solution {
	public List<List<Integer>> levelOrderBottom(TreeNode root) {
		List<List<Integer>> res = new LinkedList<List<Integer>>();
		if (root==null) {
			return res;
		}
		
		Queue<TreeNode> queue = new LinkedList<TreeNode>();
		queue.offer(root);
		while (!queue.isEmpty()) {
			int levelSize = queue.size();
			LinkedList<Integer> temp = new LinkedList<Integer>();
			for (int i = 0; i < levelSize; i++) {
				TreeNode cur = queue.poll();
				temp.offer(cur.val);
				if (cur.left != null) {
					queue.offer(cur.left);
				}
				if (cur.right != null) {
					queue.offer(cur.right);
				}
			}
			res.add(temp);
		}
		Collections.reverse(res);
		return res;
	}
}
